//
//  UIControl+clickRepeatedly.m
//  iOSBaseFramework
//
//  Created by user on 2020/6/15.
//  Copyright © 2020 com.taylor. All rights reserved.
//



#include "UIControl+TRClickRepeatedly.h"

#import <objc/runtime.h>



static const char* ClickIntervalKey;
static const char* IgnoreClick;
@implementation UIControl(TRClickRepeatedly)


-(void) setClickInterval:(NSTimeInterval)clickInterval {
    objc_setAssociatedObject(self, &ClickIntervalKey, @(clickInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(NSTimeInterval) clickInterval {
    return [objc_getAssociatedObject(self, &ClickIntervalKey) doubleValue];
}


-(void) setIgnoreClick:(BOOL)ignoreClick {
    objc_setAssociatedObject(self, &ignoreClick, @(ignoreClick), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(BOOL) ignoreClick {
    return [objc_getAssociatedObject(self, &IgnoreClick) boolValue];
}

+(void) load
{
    //替换点击事件
    Method a = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
    Method b = class_getInstanceMethod(self, @selector(rc_sendAction:to:forEvent:));
    method_exchangeImplementations(a, b);
}

- (void)rc_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
    if (self.ignoreClick) {
        return;
    }
    else
    {
        [self rc_sendAction:action to:target forEvent:event];
    }
    
    if (self.clickInterval > 0) {
        self.ignoreClick = true;
        [self performSelector:@selector(setIgnoreClick:) withObject:@(NO) afterDelay:self.clickInterval];
    }
}
@end
